﻿Imports DataAcessLayer

Public Class BL

    Dim DAL As New DAL

#Region "Back-Office"

#End Region

#Region "Front-End"

    Public Function VerificarUser(ByRef username As String, ByVal password As String) As Boolean

        Dim user = DAL.ObterUser(username, password).SingleOrDefault

        Try

            If user Is Nothing Then

                Return False

            Else

                username = user.NomeUtilizador

                Return True

            End If

        Catch ex As Exception

            Return False

        End Try

    End Function

    Public Function VerificaDados(ByVal userID As Integer, _
                                  ByVal IDTrofeu As Integer) As Boolean

        Try
            Dim voto = DAL.ObterFlagVotou(userID, IDTrofeu).FirstOrDefault

            If voto Is Nothing Then

                DAL.InserirVoto(userID, IDTrofeu, 0, False)
                Return False
            End If

            Return voto.Votou

        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Function VerificaDadosMVP(ByVal userID As Integer, _
                                     ByVal IDJogo As Integer) As Boolean

        Try

            Dim voto = DAL.ObterFlagVotouMVP(userID, IDJogo).FirstOrDefault

            If voto Is Nothing Then

                DAL.InsereVotoMVP(userID, IDJogo, 0, False)
                Return False
            End If

            Return voto.Votou

        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function Votar(ByVal userID As Integer, _
                          ByVal IDTrofeu As Integer, _
                          ByVal userIDQueVotou As Integer) As Boolean

        Try

            If DAL.ActualizaVoto(userID, IDTrofeu) Then

                Return DAL.MarcarVotou(userIDQueVotou, IDTrofeu, True)

            Else

                DAL.MarcarVotou(userIDQueVotou, IDTrofeu, True)

                Return DAL.InserirVoto(userID, IDTrofeu, 1, False)

            End If

        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Function VotarMVP(ByVal userID As Integer, _
                             ByVal IDJogo As Integer, _
                             ByVal userIDQueVotou As Integer) As Boolean

        Try

            If DAL.ActualizaVotoMVP(userID, IDJogo) Then

                Return DAL.MarcarVotouMVP(userIDQueVotou, IDJogo, True)
            Else

                DAL.MarcarVotouMVP(userIDQueVotou, IDJogo, True)

                Return DAL.InsereVotoMVP(userID, IDJogo, 1, False)
            End If

        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Function AlterarPass(ByVal PassAntiga As String, _
                                ByVal PassNova As String, _
                                ByVal userID As Integer) As Boolean

        Try
            Dim user = DAL.ObterDadosUserPorID(userID).FirstOrDefault

            If PassAntiga.ToLower <> user.Password.ToLower Then

                Return False

            Else

                If DAL.AlterarDadosUser(userID, user.UserLogin, user.NomeUtilizador, PassNova) Then

                    Return True
                End If

                Return False

            End If

        Catch ex As Exception

            Return False
        End Try
    End Function

    Public Function AlterarDados(ByVal UserName As String, ByVal DisplayName As String, ByVal PassWord As String, _
                                 ByVal IDUser As Integer) As Boolean
        Try

            Return DAL.AlterarDadosUser(IDUser, UserName, DisplayName, PassWord)

        Catch ex As Exception

            Return False
        End Try
    End Function

#End Region

End Class
